<?php

namespace App\Http\Controllers\Laifei\Work;

use App\Http\Controllers\Controller;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use App\Models\Work;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiWorkDetailController extends Controller
{

    public function index()
    {
        $all = $this->all;
        if(!isset($all['work_id'])){
            return $this->setReturnJson(-1,'缺少参数');
        }
        $work_id = $all['work_id'];
        if(!is_int($work_id)){
            return $this->setReturnJson(-1,'错误的参数类型');
        }
        try{
            $work = Work::workMessage($work_id);
            $order = DB::table('explore_order')->where('eo_id','=',$work['eo_id'])->first();
            $order = json_decode(json_encode($order), true);
            $order['shop_img'] = AliOss::getUrls(explode(',', $order['shop_img']));
            // 查询这个工单是否有红包发送记录
            $settlement = DB::table('explore_settlement')->where('work_id','=',$work_id)->get()->toArray();
            $result = [
                'work' => $work,
                'order' => $order,
                'shop' => Work::shopMessage($work['shop_id']),
                'receiver' => Work::receiverMessage($work['talent_id']),
                'log' => Work::workLog($work_id, false),
                'settlement' => $settlement,
            ];
            return $this->setReturnJson(0, 'success', $result);
        }catch (\Exception $e){
            Log::error(LaifeiWorkDetailController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败',['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }

    }

}
